#Region " Imports "

Imports BlazeApps.Library.NVLs
Imports BlazeApps.Library.Page
Imports BlazeApps.Library.WorkFlow
Imports System.Web.UI

#End Region

Partial Class Usercontrols_Management
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

    Protected PageGuid As Guid
    Protected TemplateGuid As Guid

#End Region

#Region " Page Events "

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.MessageLabel = Me.uxMessagesLabel
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        SetPageTitle("Page Management")

        If IsUserAuthenticated Then
            GetQueryStringValues()
            SetPageState()

            If Me.Page.IsPostBack = False Then

                LoadPage()

                If Not PageGuid = Guid.Empty Then
                    LoadPageValues()
                End If

                Me.uxCharCountLabel.Text = "3000"
                Me.uxSummaryTextBox.Attributes.Add("maxCharacters", "3000")
                Me.uxSummaryTextBox.Attributes.Add("onkeyup", "return CharacterCount(this, " & Me.uxCharCountLabel.ClientID & ");")
            End If

        End If

        Me.uxEditorPanel.Visible = ShowControls(SecurityMask.Editor, True) And IsAllowedItemAccess(PageGuid)

    End Sub

#End Region

#Region " Save Data "

    Private Sub SavePage()

        Dim Item As PageDetail = PageDetail.GetPageDetail(PageGuid)

        Item.pageid = PageGuid
        Item.publishdateString = Me.uxPublishDateTextBox.Text
        Item.expiredateString = Me.uxExpirationDateTextBox.Text
        Item.title = Me.uxTitleTextBox.Text.ToString.Trim
        Item.IvAccessType = Me.uxAccessTypeDropDownList.SelectedValue
        Item.IvLevel = Me.uxDifficultyLevelDropDownList.SelectedValue
        Item.IvStatus = Me.uxStatusDropDownList.SelectedValue
        Item.IvItemtype = Me.uxTypeDropDownList.SelectedValue
        Item.keywords = Me.uxKeywordsTextBox.Text.ToString.Trim
        Item.templateid = GetGuidFromSelectedItem(Me.uxEditorTemplateDropDownList)
        Item.summary = Me.uxSummaryTextBox.Text.ToString.Trim
        Item.Spotlight = Me.uxSpotLightCheckBox.Checked

        Item.Save()

    End Sub

    Private Sub CreateNewPage()
        Dim Item As PageDetail = PageDetail.NewPageDetail
        TemplateGuid = ConvertToGuid(Me.uxEditorTemplateDropDownList)

        Item.publishdateString = Me.uxPublishDateTextBox.Text
        Item.expiredateString = Me.uxExpirationDateTextBox.Text
        Item.title = Me.uxTitleTextBox.Text.ToString.Trim
        Item.IvAccessType = Me.uxAccessTypeDropDownList.SelectedValue
        Item.IvLevel = Me.uxDifficultyLevelDropDownList.SelectedValue
        Item.IvStatus = Me.uxStatusDropDownList.SelectedValue
        Item.IvItemtype = Me.uxTypeDropDownList.SelectedValue
        Item.keywords = Me.uxKeywordsTextBox.Text.ToString.Trim
        Item.templateid = GetGuidFromSelectedItem(Me.uxEditorTemplateDropDownList)
        Item.summary = Me.uxSummaryTextBox.Text.ToString.Trim
        Item.Spotlight = Me.uxSpotLightCheckBox.Checked
        Item.Userid = CurrentUserID
        Item.Save()

        uxOptions.ItemGuid = PageGuid
        uxOptions.SetInitialOptions = True
        uxOptions.SaveOptions()

        Response.Redirect(String.Format("{0}.aspx?pid=management&id={1}", AppPage, Item.pageid.ToString))

    End Sub

#End Region

#Region " Load Data "

    Private Sub LoadPage()
        LoadStatusList(Me.uxStatusDropDownList)
        LoadDifficultyLevelList(Me.uxDifficultyLevelDropDownList)
        LoadAccessTypeList(Me.uxAccessTypeDropDownList)
        LoadTemplateList(Me.uxEditorTemplateDropDownList)
        LoadItemTypeList(Me.uxTypeDropDownList)
    End Sub

    Private Sub LoadPageValues()
        Dim Items As BlazeApps.Library.Page.PageDetail = PageDetail.GetPageDetail(PageGuid)

        'General Information Section
        Me.uxPageIDLabel.Text = New String(Items.kbid.ToString)
        Me.uxCreationDateLabel.Text = Items.createdateString
        Me.uxLastActionDateLabel.Text = Items.statusdateString
        Me.uxPrivatRankingLabel.Text = Items.rank
        Me.uxPublicRankingLabel.Text = Items.rank
        Me.uxPageRevisionLabel.Text = Items.revision

        'Detailed Information Section
        Me.uxEditorTemplateDropDownList.SelectedValue = New String(Items.templateid.ToString)
        Me.uxDifficultyLevelDropDownList.SelectedValue = Items.IvLevel
        Me.uxAccessTypeDropDownList.SelectedValue = Items.IvAccessType
        Me.uxStatusDropDownList.SelectedValue = Items.IvStatus
        Me.uxTypeDropDownList.SelectedValue = Items.IvItemtype
        Me.uxTitleTextBox.Text = Items.title
        Me.uxKeywordsTextBox.Text = Items.keywords
        Me.uxSummaryTextBox.Text = Items.summary
        Me.uxPublishDateTextBox.Text = Items.publishdateString
        Me.uxExpirationDateTextBox.Text = Items.expiredateString
        Me.uxSpotLightCheckBox.Checked = Items.Spotlight

        uxMenuBuilder.PageTitle = Items.title

        uxSecurity.ItemGuid = PageGuid
        uxSecurity.ItemName = Items.title

    End Sub

#End Region

#Region " Form Controls Events "

    Protected Sub uxSaveButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxSaveButton.Click
        If PageGuid = Guid.Empty Then
            CreateNewPage()
        Else
            SavePage()
        End If

    End Sub

    Protected Sub uxEditButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxEditButton.Click

        CheckOut(PageGuid, CurrentUserID)

        Response.Redirect(String.Format("{0}.aspx?pid=editor&id={1}", AppPage, New String(PageGuid.ToString)))
    End Sub

    Protected Sub uxNewPageButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxNewPageButton.Click
        Response.Redirect(String.Format("{0}.aspx?pid=management", AppPage))
    End Sub

    Protected Sub uxPreviewButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxPreviewButton.Click
        ScriptManager.RegisterStartupScript(uxPreviewButton, Me.GetType(), "preview", "fPopUpWin('" & String.Format("{0}.aspx?pid=viewer&popup=1&preview=1&id={1}", AppPage, PageGuid.ToString) & "','preview',0 , 1, 1,800,600, 0);", True)
    End Sub

    Protected Sub uxResetButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxResetButton.Click
        If PageGuid = Guid.Empty Then
            Response.Redirect(String.Format("{0}.aspx?pid=management", AppPage))
        Else
            LoadPageValues()
        End If
    End Sub

    Protected Sub uxResourcesButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxResourcesButton.Click
        ScriptManager.RegisterStartupScript(uxResourcesButton, Me.GetType(), "resourcemgr", "fPopUpWin('" & String.Format("{0}.aspx?pid=res_resourcegallery&popup=1", AppPage) & "','resourcemgr',0 , 1, 1,790,500, 0);", True)
    End Sub

    Protected Sub uxPublishButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxPublishButton.Click
        Dim Content As PageWorkingContent = PageWorkingContent.GetPageWorkingContent(PageGuid)
        Dim Published As New PublishPage

        If Content.HasData = True Then
            Published.PushWorkingDataToLive(PageGuid)
        Else
            ScriptManager.RegisterStartupScript(uxPublishButton, Me.GetType(), "warning", "window.alert('The article does not have any content to publish.\nPlease edit the article first and then publish it.');", True)
        End If

        CheckIn(PageGuid, CurrentUserID)

    End Sub

    Protected Sub uxCheckInOutButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxCheckInOutButton.Click
        If IsCheckedOutByCurrentUser(PageGuid, CurrentUserID) = True Then
            CheckIn(PageGuid, CurrentUserID)
        Else
            CheckOut(PageGuid, CurrentUserID)
        End If

        SetPageState()
    End Sub

    Protected Sub uxDeleteButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxDeleteButton.Click
        PageDetail.DeletePageDetail(PageGuid)
        Response.Redirect(String.Format("{0}.aspx?pid=management", AppPage))
    End Sub

#End Region

#Region " Helpers and Methods "

    Private Sub GetQueryStringValues()
        PageGuid = GetGuidFromQueryString("id")
    End Sub

    Private Sub SetPageState()
        If Not PageGuid = Guid.Empty Then
            If IsCheckedOut(PageGuid) = False Then
                Me.uxCheckInOutButton.Text = Resources.Resource.CheckOut
            Else
                If IsCheckedOut(PageGuid) = True AndAlso IsCheckedOutByCurrentUser(PageGuid, CurrentUserID) = True Then
                    Me.uxCheckInOutButton.Text = Resources.Resource.CheckIn
                ElseIf IsCheckedOut(PageGuid) = True AndAlso IsCheckedOutByCurrentUser(PageGuid, CurrentUserID) = False Then
                    DisableAll(Me.Page)
                    Me.uxCheckInOutButton.Text = Resources.Resource.Locked
                End If
            End If
        Else
            Me.uxCheckInOutButton.Visible = False
        End If
    End Sub

#End Region

End Class
